<?php
namespace Common\Model;

use Common\Model\BaseModel;

class PermitModel extends BaseModel
{
    protected $tableName = 'permit';

    protected $_validate = array(
        array('apply_name', 'require', '请填写申请单位名称!'),
        array('apply_incharge', 'require', '请填写单位负责人!'),
        array('construct_name', 'require', '请填写施工单位名称！'),
    );

    public function addPermit($post)
    {

        $ret = array('status' => '', 'info' => '');

        $data = $this->returnData($post);
        if ($this->create($data)) {
            $res = null !== $data['id'] ? $this->save($data) : $this->add($data);
            if (false !== $res) {
                $ret['status'] = true;
                $ret['info'] = "Success";
            } else {
                $ret['status'] = false;
                $ret['info'] = $this->getDbError();
            }
        } else {
            $ret['status'] = false;
            $ret['info'] = $this->getError();
        }

        return $ret;
    }


    public function returnData($post)
    {
        $return['id'] = !empty($post['id']) ? intval($post['id']) : null;
        $return['tab_permit_id'] = !empty($post['tab_permit_id']) ? addslashes($post['tab_permit_id']) : $this->createTabPermitid();
        $return['apply_time'] = !empty($post['apply_time']) ? date('Y-m-d H:i:s', $post['apply_time']) : date('Y-m-d H:i:s');
        $return['status'] = !empty($post['status']) ? intval($post['status']) : 1;
        $return['remark'] = !empty($post['remark']) ? addslashes($post['remark']) : '';
        $return['staff_id'] = !empty($post['staff_id']) ? intval($post['staff_id']) : $this->staff_id;
        $return['apply_name'] = !empty($post['apply_name']) ? addslashes($post['apply_name']) : '';
        $return['apply_incharge'] = !empty($post['apply_incharge']) ? addslashes($post['apply_incharge']) : '';
        $return['construct_name'] = !empty($post['construct_name']) ? addslashes($post['construct_name']) : '';
        $return['construct_incharge'] = !empty($post['construct_incharge']) ? addslashes($post['construct_incharge']) : '';

        $return['special_cer'] = !empty($post['special_cer']) ? intval($post['special_cer']) : 2;
        $return['special_id'] = !empty($post['special_id']) ? addslashes($post['special_id']) : '';
        $return['special_time'] = !empty($post['special_time']) ? addslashes($post['special_time']) : date('Y-m-d H:i:s');

        $return['operator_name'] = !empty($post['operator_name']) ? addslashes($post['operator_name']) : '';
        $return['operator_reason'] = !empty($post['operator_reason']) ? addslashes($post['operator_reason']) : '';
        $return['operator_localtion'] = !empty($post['operator_localtion']) ? addslashes($post['operator_localtion']) : '';
        $return['operator_photo'] = !empty($post['operator_photo']) ? addslashes($post['operator_photo']) : '';
        $return['operator_type'] = !empty($post['operator_type']) ? addslashes($post['operator_type']) : '';
        $return['operator_starttime'] = !empty($post['operator_starttime']) ? addslashes($post['operator_starttime']) : date('Y-m-d H:i:s');
        $return['operator_endtime'] = !empty($post['operator_endtime']) ? addslashes($post['operator_endtime']) : date('Y-m-d H:i:s');
        $return['operator_measure'] = !empty($post['operator_measure']) ? addslashes($post['operator_measure']) : '';
        $return['is_drift'] = !empty($post['is_drift']) ? $post['is_drift'] : 1;

        return $return;
    }

    public function createTabPermitid()
    {

        $tab_permit_id = date('YmdHis', time());
        return $tab_permit_id;
    }

    public function getList()
    {

        $list = $this->select();
        return $list;
    }

}